package com.emser.unc.client.view;

import com.emser.unc.client.CloudMobile;
import com.emser.unc.client.presenter.SaleReportDetailPresenter;
import com.emser.unc.serializable.ProductSoldDTO;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;

public class SaleReportDetailView extends Composite implements SaleReportDetailPresenter.Display {
	private CellTable<ProductSoldDTO> productsSoldTable;
	private SimplePager pager;
	private VerticalPanel verticalPanel;
	private Button btnGoBack;
	private SimplePanel simplePanel;
	private Label sellerUName, sellerName, sellerAddress, sellerTelphone;
	private Label customerID, customerName,  customerAddress, customerTelephone;
	private BusyIndicatorView busyIndicator = new BusyIndicatorView(CloudMobile.constants.msjWorking());
	private Label lblMsgCDeleted;
	private Label lblMsgSDeleted;
	private VerticalPanel verticalPanel_1;
	private ScrollPanel scrollPanel;
	private int pageSize = CloudMobile.constants.pageSizeCP();
	private int heightCPanel;
	private int heightSPanel;
	public SaleReportDetailView(){
		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("100%", CloudMobile.get().getHeightPanelCenter() + "px");
		heightCPanel = CloudMobile.get().getHeightPanelCenter() - 100;
		heightSPanel = heightCPanel - 246;
		initHPanelTitle();
		initScrollPanel();	
	    initPager();
	    busyIndicator.hide();
	}
	
	private void initScrollPanel() { 
	    verticalPanel_1 = new VerticalPanel();
	    verticalPanel_1.setStyleName("gwt-vPanelSRDetail");
	    verticalPanel.add(verticalPanel_1);
	    verticalPanel.setCellHeight(verticalPanel_1, heightCPanel+"px");
	    verticalPanel.setCellWidth(verticalPanel_1, "100%");
	    verticalPanel_1.setSize("100%", heightCPanel+"px");
	    
	    Grid grid = new Grid(9, 3);
	    verticalPanel_1.add(grid);
	    verticalPanel_1.setCellHeight(grid, "216px");
	    verticalPanel_1.setCellWidth(grid, "100%");
	    verticalPanel.setCellVerticalAlignment(grid, HasVerticalAlignment.ALIGN_MIDDLE);
	    verticalPanel.setCellHeight(grid, "18px");
	    verticalPanel.setCellHorizontalAlignment(grid, HasHorizontalAlignment.ALIGN_CENTER);
	    grid.setSize("440px", "216px");
	    
	    Label lblSeller = new Label(CloudMobile.constants.seller()+":");
	    lblSeller.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    grid.setWidget(0, 0, lblSeller);
	    grid.getCellFormatter().setHeight(0, 0, "20px");
	    grid.getCellFormatter().setStyleName(0, 0, "gwt-LabelTitleDetail");
	    grid.getCellFormatter().setWidth(0, 0, "75px");
	    lblSeller.setSize("75px", "18px");
	    
	    sellerUName = new Label("");
	    sellerUName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    grid.setWidget(0, 1, sellerUName);
	    grid.getCellFormatter().setHeight(0, 1, "20px");
	    sellerUName.setSize("155px", "18px");
	    grid.getCellFormatter().setWidth(0, 1, "155px");
	    
	    lblMsgSDeleted = new Label(CloudMobile.constants.msgSDeleted());
	    lblMsgSDeleted.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    lblMsgSDeleted.setVisible(false);
	    grid.setWidget(0, 2, lblMsgSDeleted);
	    grid.getCellFormatter().setHeight(0, 2, "20px");
	    lblMsgSDeleted.setSize("200px", "18px");
	    grid.getCellFormatter().setWidth(0, 2, "300px");
	    
	    Label lblNameSeller = new Label(CloudMobile.constants.name()+":");
	    lblNameSeller.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    grid.setWidget(1,0,lblNameSeller);
	    grid.getCellFormatter().setHeight(1, 0, "20px");
	    grid.getCellFormatter().setStyleName(1, 0, "gwt-LabelDetail");
	    lblNameSeller.setSize("75px", "18px");
	    grid.getCellFormatter().setWidth(1, 0, "75px");
	    sellerName = new Label("");
	    sellerName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    sellerName.setStyleName("gwt-LabelDetail-1");
	    grid.setWidget(1, 1, sellerName);
	    grid.getCellFormatter().setHeight(1, 1, "20px");
	    sellerName.setSize("155px", "18px");
	    grid.getCellFormatter().setWidth(1, 1, "155px");
	    
	    Label lblAddrSeller = new Label(CloudMobile.constants.address()+":");
	    lblAddrSeller.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    grid.setWidget(2, 0, lblAddrSeller);
	    grid.getCellFormatter().setStyleName(2, 0, "gwt-LabelDetail");
	    grid.getCellFormatter().setHeight(2, 0, "20px");
	    lblAddrSeller.setSize("75px", "18px");
	    grid.getCellFormatter().setWidth(2, 0, "75px");
	    sellerAddress = new Label("");
	    sellerAddress.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    sellerAddress.setStyleName("gwt-LabelDetail-1");
	    grid.setWidget(2, 1, sellerAddress);
	    grid.getCellFormatter().setHeight(2, 1, "20px");
	    sellerAddress.setSize("155px", "18px");
	    grid.getCellFormatter().setWidth(2, 1, "155px");
	    Label lblTelSeller = new Label(CloudMobile.constants.telephone()+":");
	    lblTelSeller.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    grid.setWidget(3, 0, lblTelSeller);
	    grid.getCellFormatter().setStyleName(3, 0, "gwt-LabelDetail");
	    grid.getCellFormatter().setHeight(3, 0, "22px");
	    grid.getCellFormatter().setWidth(3, 0, "75px");
	    lblTelSeller.setSize("75px", "18px");
	    sellerTelphone = new Label("");
	    sellerTelphone.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    sellerTelphone.setStyleName("gwt-LabelDetail-1");
	    grid.setWidget(3, 1, sellerTelphone);
	    grid.getCellFormatter().setHeight(3, 1, "20px");
	    sellerTelphone.setSize("155px", "18px");
	    grid.getCellFormatter().setWidth(3, 1, "155px");
	    
	    Label lblCustomer = new Label(CloudMobile.constants.customer()+":");
	    lblCustomer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    grid.setWidget(4, 0, lblCustomer);
	    grid.getCellFormatter().setStyleName(4, 0, "gwt-LabelTitleDetail");
	    grid.getCellFormatter().setHeight(4, 0, "20px");
	    grid.getCellFormatter().setWidth(4, 0, "75px");
	    lblCustomer.setSize("75px", "18px");
	    
	    customerID = new Label("");
	    customerID.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    grid.setWidget(4, 1, customerID);
	    grid.getCellFormatter().setHeight(4, 1, "20px");
	    grid.getCellFormatter().setWidth(4, 1, "155px");
	    customerID.setSize("155px", "18px");
	    
	    lblMsgCDeleted = new Label(CloudMobile.constants.msgCDeleted());
	    lblMsgCDeleted.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    lblMsgCDeleted.setVisible(false);
	    grid.setWidget(4, 2, lblMsgCDeleted);
	    grid.getCellFormatter().setHeight(4, 2, "20px");
	    grid.getCellFormatter().setWidth(4, 2, "300px");
	    lblMsgCDeleted.setSize("200px", "18px");
	    
	    Label lblNameCustomer = new Label(CloudMobile.constants.name()+":");
	    lblNameCustomer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    lblNameCustomer.setStyleName("gwt-LabelDetail");
	    grid.setWidget(5, 0, lblNameCustomer);
	    grid.getCellFormatter().setHeight(5, 0, "20px");
	    lblNameCustomer.setSize("75px", "18px");
	    grid.getCellFormatter().setWidth(5, 0, "75px");
	    
	    Label lblAddressCustomer = new Label(CloudMobile.constants.address()+":");
	    lblAddressCustomer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    lblAddressCustomer.setStyleName("gwt-LabelDetail");
	    grid.setWidget(6, 0, lblAddressCustomer);
	    grid.getCellFormatter().setHeight(6, 0, "20px");
	    lblAddressCustomer.setSize("75px", "18px");
	    grid.getCellFormatter().setWidth(6, 0, "75px");
	    
	    Label lblTelCustomer = new Label(CloudMobile.constants.telephone()+":");
	    lblTelCustomer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    lblTelCustomer.setStyleName("gwt-LabelDetail");
	    grid.setWidget(7, 0, lblTelCustomer);
	    grid.getCellFormatter().setHeight(7, 0, "20px");
	    lblTelCustomer.setSize("75px", "18px");
	    grid.getCellFormatter().setWidth(7, 0, "75px");
	    
	    customerName = new Label("");
	    customerName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    customerName.setStyleName("gwt-LabelDetail-1");
	    grid.setWidget(5, 1, customerName);
	    grid.getCellFormatter().setHeight(5, 1, "20px");
	    customerName.setSize("155px", "18px");
	    grid.getCellFormatter().setWidth(5, 1, "155px");
	    
	    customerAddress = new Label("");
	    customerAddress.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    customerAddress.setStyleName("gwt-LabelDetail-1");
	    grid.setWidget(6, 1, customerAddress);
	    grid.getCellFormatter().setHeight(6, 1, "20px");
	    customerAddress.setSize("155px", "18px");
	    grid.getCellFormatter().setWidth(6, 1, "155px");
	    
	    customerTelephone = new Label("");
	    customerTelephone.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    customerTelephone.setStyleName("gwt-LabelDetail-1");
	    grid.setWidget(7, 1, customerTelephone);
	    grid.getCellFormatter().setHeight(7, 1, "20px");
	    customerTelephone.setSize("155px", "18px");
	    grid.getCellFormatter().setWidth(7, 1, "155px");
	    
	    Label lblProducts = new Label(CloudMobile.constants.products());
	    lblProducts.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    lblProducts.setStyleName("gwt-LabelTitleDetail");
	    grid.setWidget(8, 0, lblProducts);
	    grid.getCellFormatter().setHeight(8, 0, "25px");
	    lblProducts.setSize("75px", "18px");
	    grid.getCellFormatter().setWidth(8, 0, "75px");
	    grid.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setVerticalAlignment(4, 0, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(4, 2, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(4, 2, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(1, 1, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setVerticalAlignment(2, 0, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setVerticalAlignment(3, 0, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(5, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(5, 0, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(6, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(6, 0, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(7, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(7, 0, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(8, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(8, 0, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(2, 1, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(3, 1, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(4, 1, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(4, 1, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(5, 1, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(5, 1, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(6, 1, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(6, 1, HasVerticalAlignment.ALIGN_BOTTOM);
	    grid.getCellFormatter().setHorizontalAlignment(7, 1, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setVerticalAlignment(7, 1, HasVerticalAlignment.ALIGN_BOTTOM);
	    initColumnsTable();
	}
	private void initPager() {
		simplePanel = new SimplePanel();
	    verticalPanel.add(simplePanel);
	    verticalPanel.setCellVerticalAlignment(simplePanel, HasVerticalAlignment.ALIGN_BOTTOM);
	    simplePanel.setSize("400px", "40px");
	    verticalPanel.setCellHorizontalAlignment(simplePanel, HasHorizontalAlignment.ALIGN_CENTER);
	    verticalPanel.setCellWidth(simplePanel, "100%");
	    verticalPanel.setCellHeight(simplePanel, "40px");
	    pager = new SimplePager(TextLocation.CENTER){
	    	@Override
	    	public void setPageStart(int index) {
	    	  if (getDisplay() != null) {
	    	    Range range = getDisplay().getVisibleRange();
	    	    int pageSize = range.getLength();
	    	    index = Math.max(0, index);
	    	    if (index != range.getStart()) {
	    	      getDisplay().setVisibleRange(index, pageSize);
	    	    }
	    	  }
	    	}
	    };
	    pager.setStyleName("gwt-pager1");
	    pager.setStylePrimaryName("gwt-pager1");
	    pager.setRangeLimited(true);
	    pager.setDisplay(productsSoldTable);
	    pager.setSize("400px", "15px");
	    pager.setPageSize(pageSize);
	    pager.setPage(0);
	    simplePanel.add(pager);
	}
	private void initColumnsTable() {
		scrollPanel = new ScrollPanel();
	    verticalPanel_1.add(scrollPanel);
	    verticalPanel_1.setCellHorizontalAlignment(scrollPanel, HasHorizontalAlignment.ALIGN_CENTER);
	    verticalPanel_1.setCellHeight(scrollPanel, heightSPanel+"px");
	    verticalPanel_1.setCellWidth(scrollPanel, "100%");
	    scrollPanel.setStyleName("gwt-sPanelCReports");
	    scrollPanel.setSize("100%-40px", heightSPanel+"px");
	    productsSoldTable= new CellTable<ProductSoldDTO>();
	    
	    productsSoldTable.setSize("100%", "100%");
	    scrollPanel.setWidget(productsSoldTable);
	    verticalPanel.setCellVerticalAlignment(scrollPanel, HasVerticalAlignment.ALIGN_BOTTOM);
	    verticalPanel.setCellHorizontalAlignment(scrollPanel, HasHorizontalAlignment.ALIGN_CENTER);
	    verticalPanel.setCellWidth(scrollPanel, "100%");
	    verticalPanel.setCellHeight(scrollPanel, heightSPanel+"px");
	    TextColumn<ProductSoldDTO> tittleColumn = new TextColumn<ProductSoldDTO>() {
	      @Override
	      public String getValue(ProductSoldDTO object) {
	        return object.getTitle();
	      }
	    };
	    tittleColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
	    productsSoldTable.addColumn(tittleColumn, CloudMobile.constants.title());  
	    TextColumn<ProductSoldDTO> priceColumn = new TextColumn<ProductSoldDTO>() {
		      @Override
		      public String getValue(ProductSoldDTO object) {
		        return object.getUnitPrice().toString();
		      }
		    };
		priceColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		productsSoldTable.addColumn(priceColumn, CloudMobile.constants.price()); 
	    TextColumn<ProductSoldDTO> quantityColumn = new TextColumn<ProductSoldDTO>() {
	      @Override
	      public String getValue(ProductSoldDTO object) {
	        return object.getQuantity().toString();
	      }
	    };
	    quantityColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
	    productsSoldTable.addColumn(quantityColumn, CloudMobile.constants.quantity());
	    
	    TextColumn<ProductSoldDTO> totalPriceColumn = new TextColumn<ProductSoldDTO>() {
		      @Override
		      public String getValue(ProductSoldDTO object) {
		    	Double total = object.getUnitPrice() * object.getQuantity();  
		        return total.toString();
		      }
		    };
	    totalPriceColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
	    productsSoldTable.addColumn(totalPriceColumn, CloudMobile.constants.amount());	
	
	}
	private void initHPanelTitle() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setStyleName("gwt-hPanelCenter");
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
	    horizontalPanel.setSize("100%", "100%");
	    
	    btnGoBack = new Button(CloudMobile.constants.goBack());
	    horizontalPanel.add(btnGoBack);
	    btnGoBack.setSize("112", "28");
	    horizontalPanel.setCellVerticalAlignment(btnGoBack, HasVerticalAlignment.ALIGN_MIDDLE);
	    verticalPanel.add(horizontalPanel);  
	    verticalPanel.setCellHeight(horizontalPanel, "40px");
	    verticalPanel.setCellWidth(horizontalPanel, "100%");
	}

	@Override
	public HasData<ProductSoldDTO> getTable() {
		return productsSoldTable;
	}
	@Override
	public HasClickHandlers getGoBackButton() {
		return btnGoBack;
	}

	@Override
	public HasText getSellerName() {
		return sellerName;
	}


	@Override
	public HasText getSellerAddress() {
		return sellerAddress;
	}

	@Override
	public HasText getSellerTelephone() {
		return sellerTelphone;
	}

	@Override
	public HasText getSellerUName() {
		return sellerUName;
	}

	@Override
	public HasText getCustomerName() {
		return customerName;
	}


	@Override
	public HasText getCustomerAddress() {
		return customerAddress;
	}

	@Override
	public HasText getCustomerTelephone() {
		return customerTelephone;
	}

	@Override
	public HasText getCustomerID() {
		return customerID;
	} 
	@Override
	public void showBusyIndicator(){
		busyIndicator.show();
	}
	@Override
	public void hideBusyIndicator(){
		busyIndicator.hide();
	}

	@Override
	public void enableMsgCDeleted(boolean enable) {
		lblMsgCDeleted.setVisible(enable);
	}

	@Override
	public void enableMsgSDeleted(boolean enable) {
		lblMsgSDeleted.setVisible(enable);
	}

	@Override
	public void setPage(int page) {
		pager.setPage(page);
	}

	@Override
	public int getPage() {
		return pager.getPage();
	}
	
	@Override
	public Widget asWidget() {
		return this;
	}

}
